package demo2interface;

import java.util.Arrays;
import java.util.Comparator;

//实现接口1
class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return (this.name).compareTo(o.name);
    }
}
class AgeComparator implements Comparator<Student> {
    //实现Compatator接口
    @Override
    public int compare(Student o1, Student o2) {
        //按年龄比较
        return o1.age - o2.age;
    }
}
class NameComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void mysort(Comparable[] comparable) {
        //自己实现Arrays.sort排序
        for (int i = 0; i < comparable.length-1; i++) {
            for (int j = 0; j < comparable.length-1-i; j++) {
                if(comparable[j].compareTo(comparable[j+1])>0) {
                    //交换
                    Comparable tmp = comparable[j];
                    comparable[j] = comparable[j+1];
                    comparable[j+1] = tmp;
                }
            }
        }


    }

    public static void main(String[] args) {
        //使用比较器
        Student student1 = new Student("zhangsan",18);
        Student student2 = new Student("lisi",99);
        //实现对象再调用比较器
        AgeComparator agecomparator = new AgeComparator();
        System.out.println(agecomparator.compare(student1,student2));

       NameComparator namecomparator = new NameComparator();
        System.out.println(namecomparator.compare(student1, student2));
        System.out.println("-------------");
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",18);
        students[1] = new Student("lisi",28);
        students[2] = new Student("wangwu",66);

        AgeComparator agecomparator2 = new AgeComparator();

        Arrays.sort(students,agecomparator2);
        System.out.println(Arrays.toString(students));

        NameComparator namecomparator2 = new NameComparator();
        Arrays.sort(students,namecomparator2);
        System.out.println(Arrays.toString(students));

    }
    public static void main3(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",18);
        students[1] = new Student("lisi",28);
        students[2] = new Student("wangwu",66);
        System.out.println(Arrays.toString(students));
        System.out.println("--------");
        mysort(students);
        System.out.println(Arrays.toString(students));

    }
    public static void main2(String[] args) {
        //使用Arrays.sort排序
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",18);
        students[1] = new Student("lisi",28);
        students[2] = new Student("wangwu",66);
        Arrays.sort(students);//排序时会将元素类型强转为Comparab类型
        //

        System.out.println(Arrays.toString(students));

    }
    public static void main1(String[] args) {
        //简单排序
        Student student1 = new Student("zhangsan",18);
        Student student2 = new Student("lisi",28);

        if(student1.compareTo(student2) > 0) {
            System.out.println("studen1 > studen2");
        }else {
            System.out.println("studen1 <= studen2");

        }


        /*String str1 = "zhangsan";
        String str2 = "lisi";
        if(str1.compareTo(str2) > 0) {
            System.out.println("str1>str2");
        }else {
            System.out.println("str1<=str2");

        }*/


    }

}
